<?php
namespace App\Wen\Utils;

use Illuminate\Support\Facades\DB;

class TenantUtils{
    public static function tenant_change_id_all_tables($old_tenant_id, $new_tenant_id){
        $tables = DB::select('SHOW TABLES');
        $tableNames = array_map('current', $tables);
        if($tableNames){
            foreach ($tableNames as $table_name){
                if(StrUtils::startsWith($table_name, 'admin_')){
                    continue;
                }
                if(!StrUtils::startsWith($table_name, 'wx_') && !StrUtils::startsWith($table_name, 'mini_')){
                    continue;
                }
                $table_info_columns = DB::select( DB::raw('SHOW COLUMNS FROM '.$table_name));
                if(in_array('tenant_id', $table_info_columns)){
                    DB::select("UPDATE ".$table_name." SET tenant_id = '.$new_tenant_id.' WHERE tenant_id = ".$old_tenant_id);
                }
            }
        }
    }
}
